package com.example.test1.adapter;

import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.test1.R;
import com.example.test1.bean.Lists;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 段傅华 on 2018/12/12.
 */

public class RlvAdapter extends RecyclerView.Adapter{
    private  Context mContext;
    private  ArrayList<Lists.DataBean.NewListBean> mList;
    private OnItemClickListener mListstener;

    public RlvAdapter(ArrayList<Lists.DataBean.NewListBean> newList, Context context) {
        this.mList=newList;
        this.mContext=context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlayout, null, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflate);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder holder1= (MyViewHolder) holder;
        holder1.mTv.setText(mList.get(position).getTitle());
        Glide.with(mContext).load(mList.get(position).getImageListThumb().get(0)).placeholder(R.mipmap.ic_launcher).into(holder1.mIv);
        holder1.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListstener.OnItemClick(v,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addData(List<Lists.DataBean.NewListBean> newList) {
        mList.addAll(newList);
        notifyDataSetChanged();
    }
    class MyViewHolder extends RecyclerView.ViewHolder{

        final ImageView mIv;
        final TextView mTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            mIv = itemView.findViewById(R.id.item_iv);
            mTv = itemView.findViewById(R.id.item_tv);
        }
    }
    //点击监听
    public interface OnItemClickListener{
        void OnItemClick(View v,int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListstener=listener;
    }
}
